import { GetRangePickerName } from './utils';
import { IGaeaAll, IGaeaInstance } from '../../types';

const formPermissionHandler = (
  formObj: IGaeaAll,
  item: IGaeaInstance,
  others?: any
) => {
  if (!item.childs) return {};
  return {
    required: formObj[item.childs[0]].data.props.required,
    subName: others.parentName
      ? `${formObj[item.childs[0]].data.props.name}-${
        formObj[item.childs[1]].data.props.name
      }`
      : '',
    name: others.parentName
      ? others.parentName
      : `${formObj[item.childs[0]].data.props.name}-${
        formObj[item.childs[1]].data.props.name
      }`,
    disableEdit: false
  };
};

// will change formJson
const beforeSubmitHandler = function(
  formJson: IGaeaAll,
  childInstance: IGaeaInstance
) {
  // 如果是日期区间类型 则name为拼装起来的
  const dateItemId = childInstance.childs?.[0];
  const durationItemId = childInstance.childs?.[2];
  if (dateItemId && durationItemId) {
    const durationItem = formJson[durationItemId];
    const dateItem = formJson[dateItemId];
    durationItem.data.props.name = GetRangePickerName({
      originName: durationItem.data.props.originName,
      mode: dateItem.data.props.mode
    });
  }
  if (!childInstance.data.props.isAutoCalc && durationItemId) {
    const durationItem = formJson[durationItemId];
    durationItem.data.props.copyCustomCode = durationItem.data.props.customCode;
    // delete durationItem.data.props.type; // 删除类型，后台不返回给数据报表
    delete durationItem.data.props.code; // 删除类型，后台不返回给数据报表
    delete durationItem.data.props.customCode; // 删除类型，后台不返回给数据报表
  }
};

// will change formJson
const beforeParseDetailHandler = function(
  formJson: IGaeaAll,
  childInstance: IGaeaInstance
) {
  // 如果是日期区间类型 则name为拼装起来的
  const durationItemId = childInstance.childs?.[2];
  if (durationItemId && !childInstance.data.props.isAutoCalc) {
    const durationItem = formJson[durationItemId];
    // durationItem.data.props.type = 'number';
    durationItem.data.props.code = durationItemId; // 删除类型，后台不返回给数据报表
    durationItem.data.props.customCode = durationItem.data.props.copyCustomCode;
    delete durationItem.data.props.copyCustomCod;
  }
};

export const RangePickerExtraSetting = {
  formPermissionHandler,
  beforeSubmitHandler,
  beforeParseDetailHandler
};
